import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: '首页',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile/index',
    component: Layout,
    children: [
      {
        path: 'center',
        component: () => import('@/views/center/index'),
        name: '个人中心',
        hidden: true,
        meta: { title: '个人中心', icon: 'dashboard' }
      },
      {
        path: 'password',
        component: () => import('@/views/center/password'),
        name: '密码修改',
        hidden: true,
        meta: { title: '密码修改', icon: 'dashboard' }
      }
    ]
  }
]

export const asyncRoutes = [
  {
    path: '/user',
    component: Layout,
    meta: { title: '用户管理', icon: 'documentation', roles: ['admin'], breadcrumb: false },
    children: [
      {
        path: '/user/info',
        name: '用户信息',
        component: () => import('@/views/user/info'),
        meta: { title: '用户信息', icon: 'edit', roles: ['admin'] }
      },
      {
        path: '/user/create',
        name: '创建用户',
        hidden: true,
        component: () => import('@/views/user/info'),
        meta: { title: '创建用户', icon: 'edit', roles: ['admin'] }
      },
      {
        path: '/user/student',
        name: '学生课程信息',
        component: () => import('@/views/course/student'),
        meta: { title: '学生课程信息', icon: 'peoples', roles: ['admin'] }
      },
      {
        path: '/user/comment',
        name: '在线留言管理',
        component: () => import('@/views/onlinemessage/index'),
        meta: { title: '在线留言', icon: 'edit', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/resource',
    component: Layout,
    meta: { title: '资源管理', icon: 'documentation', roles: ['admin'], breadcrumb: false },
    children: [
      {
        path: '/course/resource',
        name: '资源上传',
        component: () => import('@/views/resource/resource'),
        meta: { title: '资源上传', icon: 'example', roles: ['admin'] }
      },
      {
        path: '/resource/info',
        name: '资源信息',
        component: () => import('@/views/resource/resourceManage'),
        meta: { title: '资源信息', icon: 'edit', roles: ['admin'] }
      },
      {
        path: '/course/problem',
        name: '题库管理',
        component: () => import('@/views/course/problem'),
        meta: { title: '题库管理', icon: 'edit', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/course',
    component: Layout,
    meta: { title: '课程管理', icon: 'list', roles: ['editor'], breadcrumb: false },
    children: [
      {
        path: '/course/create',
        name: '课程信息',
        component: () => import('@/views/course/create'),
        meta: { title: '课程信息', icon: 'education', roles: ['editor'] }
      },
      {
        path: '/course/edit',
        hidden: true,
        component: () => import('@/views/course/edit'),
        meta: { title: '课程信息', icon: 'education', roles: ['editor'] }
      },
      {
        path: '/course/teachplan',
        name: '课程计划',
        component: () => import('@/views/course/teachplan'),
        meta: { title: '课程计划', icon: 'tree-table', roles: ['editor'] }
      },
      {
        path: '/course/task',
        name: '作业批改',
        hidden: true,
        component: () => import('@/views/course/task'),
        meta: { title: '作业批改', icon: 'edit', roles: ['editor'] }
      },
      {
        path: '/course/student',
        name: '学生管理',
        component: () => import('@/views/course/student'),
        meta: { title: '学生管理', icon: 'peoples', roles: ['editor'] }
      },
      {
        path: '/course/resource',
        name: '资源上传',
        component: () => import('@/views/resource/resource'),
        meta: { title: '资源上传', icon: 'example', roles: ['editor'] }
      },
      {
        path: '/course/resource/manage',
        name: '资源管理',
        component: () => import('@/views/resource/resourceManage'),
        meta: { title: '资源管理', icon: 'edit', roles: ['editor'] }
      },
      {
        path: '/course/problem',
        name: '题目管理',
        component: () => import('@/views/course/problem'),
        meta: { title: '题目管理', icon: 'edit', roles: ['editor'] }
      },
      {
        path: '/user/comment',
        name: '在线留言管理',
        component: () => import('@/views/onlinemessage/index'),
        meta: { title: '在线留言', icon: 'edit', roles: ['editor'] }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
